package cn.edu.zut.controller;

import cn.edu.zut.domin.BookLendInfo;
import cn.edu.zut.domin.Reader;
import cn.edu.zut.service.IBookLendInfoService;
import cn.edu.zut.service.IReaderService;
import com.github.pagehelper.PageInfo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/bookLendInfo2")
public class BookLendInfoController2 {
    ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
    IReaderService readerService = (IReaderService) ac.getBean("readerServiceImpl") ;
    IBookLendInfoService bookLendInfoService =(IBookLendInfoService)ac.getBean("bookLendInfoServiceImpl");
    @RequestMapping("/findAll")
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Reader> readers = readerService.findAll(page,size);
        PageInfo<Reader> pageInfo = new PageInfo<>(readers);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("LentReaderInfo");
        return mv;
    }

    @RequestMapping("/findByReader")
    public ModelAndView findByReader(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                               @RequestParam(name = "size", required = true, defaultValue = "4") int size,String readerId) throws Exception {
        ModelAndView mv = new ModelAndView();
        System.out.println(readerId);
        List<BookLendInfo> bookLendInfos = bookLendInfoService.findBookLendInfoByReader(page, size,readerId);
        PageInfo<BookLendInfo> pageInfo = new PageInfo<>(bookLendInfos);
        //总页数....
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("LentReaderInfoDetail");
        return mv;
    }

    @RequestMapping("/findReaderById")
    public ModelAndView findReaderById(String readerId) throws Exception {
        System.out.println(readerId);
        ModelAndView mv = new ModelAndView();
        List<Reader> readers = readerService.findReaderById(readerId);
        mv.addObject("readerList", readers);
        mv.setViewName("LentReaderInfo");
        return mv;
    }
    @RequestMapping("/save")
    public String saveLendInfo(BookLendInfo bookLendInfo) throws Exception {
        bookLendInfoService.saveBookLendInfo(bookLendInfo);
        return "redirect:findBookLendInfoAll";
    }

}
